package com.xuecheng.checkcode.service.impl;

import com.xuecheng.checkcode.service.CheckCodeService;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author 勾新杰
 * @version 1.0
 * @description 数字字母生成器
 * @date 2022/9/29 18:28
 */
@Component("NumberLetterCheckCodeGenerator")
public class NumberLetterCheckCodeGenerator implements CheckCodeService.CheckCodeGenerator {

    private static final char[] NUMBER_LETTER = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();

    /**
     * 生成验证码
     *
     * @param length 验证码长度
     * @return 验证码
     */
    @Override
    public String generate(int length) {
        Random random = new Random();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(NUMBER_LETTER.length);
            builder.append(NUMBER_LETTER[number]);
        }
        return builder.toString();
    }
}
